﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;

namespace WebApp.Management.Product
{
    public partial class ProductComment : PageBase2
    {
        CommentManager cm = new CommentManager();
        ProductManager pm = new ProductManager();
        private RoleManager m_RoleManager = new RoleManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.SignInUser1 == null)
            {
                Session.Remove("SignInUser1");
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请先登录权限!');window.top.location.href='/Management/indexs.aspx';", true);
                return;
            }
            if (!IsPostBack)
            {
                Cosmetic.DataEntity.User theUser = this.SignInUser1;
                if (!this.m_RoleManager.CheckUserRole(theUser, RoleManager.ERole.Admin))
                {
                    Session.Remove("SignInUser1");
                    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('你没有管理员权限!');window.top.location.href='/Management/indexs.aspx';", true);
                    return;
                }
            }
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(Request.QueryString["PID"]))
                {
                    return;
                }

                LoadComment();
            }
        }

        private void LoadComment()
        {
            string pid = Request.QueryString["PID"];
            Cosmetic.DataEntity.Product pp = pm.GetProductById(new Guid(pid));
            this.Label1.Text = pp.Name;
            this.Label2.Text = pp.ProductCode;

            this.gvComment.DataSource = cm.GetAllCommentByProduct(pp);
            this.gvComment.DataBind();
        }

        protected void gvComment_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.gvComment.EditIndex = e.NewEditIndex;
            LoadComment();
        }

        protected void gvComment_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            HiddenField hfCommentId = gvComment.Rows[e.RowIndex].FindControl("hfCommentId") as HiddenField;
            Cosmetic.DataEntity.Comment thecomment = cm.GetCommentById(new Guid(hfCommentId.Value));

            cm.Delete(thecomment);
            LoadComment();
        }

        protected void gvComment_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox theContent = gvComment.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;

            HiddenField hfCommentId = gvComment.Rows[e.RowIndex].FindControl("hfCommentId") as HiddenField;
            Cosmetic.DataEntity.Comment thecomment = cm.GetCommentById(new Guid(hfCommentId.Value));

            thecomment.Content = theContent.Text.Trim();
            cm.Update();
            this.gvComment.EditIndex = -1;
            LoadComment();
        }

        protected void gvComment_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.gvComment.EditIndex = -1;
            LoadComment();
        }

        protected void gvComment_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvComment.PageIndex = e.NewPageIndex;
            LoadComment();
        }
    }
}